﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EuroAudit
{
    public partial class Options : System.Web.UI.Page
    {
        GlobalF GFunc = new GlobalF();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Session["UserName"].ToString()) | Session["UserName"].ToString() == "guest")
            {
                Response.Redirect("Login.aspx", true);
                //return;
            }
            if (Session["IsAdmin"].ToString() == "1")
            {
                cmdNew.Visible = true;
                //cmdNewU.Visible = true;
            }
            else
            {
                return;
                //cmdNew.Visible = false;
                //cmdNewU.Visible = false;
            }
            if (IsPostBack)
            {
                if (_RESULT.Text == "OKcmdDel")
                {
                    cmdDel_Click(sender, e);
                    _RESULT.Text = "";
                }
            }

            LoadOpt();
        }

        void LoadOpt()
        {
            DataSet dsList;
            //DataSet dsGroupList;
            TableHeaderCell thcNewCell;
            TableHeaderRow thrNewRow;
            TableCell tcNewCell;
            TableRow trNewRow;
            Button cmdEdit;
            /*string LastStruct = "";
            string CurrStruct = "";
            string LastNod = "";
            string CurrNod = "";
            */
            //Button cmdGEdit;
            //string UsersInGroup;

            tOptions.Rows.Clear();
            //
            string QStr = "SELECT RecID, OptType, OptUser, OptGroup, OptName, OptValue, OptDescr FROM options WHERE RecStatus=0 AND OptType='GLOBAL' ORDER BY OptGroup, OptName";
            dsList = GFunc.GetData(QStr, "WebCNS", false);
            if (dsList.Tables.Count > 0)
            {
                if (dsList.Tables[0].Rows.Count > 0)
                {
                    thrNewRow = new TableHeaderRow();
                    thcNewCell = new TableHeaderCell();
                    thcNewCell.Text = "Labot";
                    thrNewRow.Cells.Add(thcNewCell);
                    foreach (DataColumn Col in dsList.Tables[0].Columns)
                    {
                        thcNewCell = new TableHeaderCell();
                        thcNewCell.Text = Col.ColumnName;
                        thrNewRow.Cells.Add(thcNewCell);
                    }
                    tOptions.Rows.AddAt(0, thrNewRow);

                    //CurrStruct = row["Strukturvieniba"].ToString();
                    //CurrNod = row["Nodala"].ToString();

                    foreach (DataRow row in dsList.Tables[0].Rows)
                    {
                        trNewRow = new TableRow();
                        tcNewCell = new TableCell();
                        if (Session["IsAdmin"].ToString() == "1")
                        {
                            cmdEdit = new Button();
                            cmdEdit.ID = "RecEdit" + row["RecID"].ToString();
                            cmdEdit.Text = "Labot";
                            cmdEdit.CssClass = "cmdEdit";
                            cmdEdit.CommandArgument = row["RecID"].ToString();
                            cmdEdit.PostBackUrl = "#pEdit";
                            cmdEdit.Click += new EventHandler(cmdEdit_Click);
                            tcNewCell.Controls.Add(cmdEdit);
                            Label BkmListG = new Label();
                            BkmListG.Text = "<a name=\"" + row["RecID"].ToString() + "\"></a>";
                            //BkmListG.Visible = false;
                            tcNewCell.Controls.Add(BkmListG);
                        }
                        trNewRow.Cells.Add(tcNewCell);
                        foreach (DataColumn Col in dsList.Tables[0].Columns)
                        {
                            tcNewCell = new TableCell();
                            tcNewCell.Text = row[Col].ToString() + "";
                            trNewRow.Cells.Add(tcNewCell);
                        }
                        tOptions.Rows.Add(trNewRow);

                    }
                }
            }
        }

        void cmdEdit_Click(object sender, EventArgs e)
        {

            string Rec2Edit;
            string QString;
            DataSet ds;

            System.Web.UI.WebControls.Button b = sender as System.Web.UI.WebControls.Button;
            Rec2Edit = b.ID.Substring(7);
            QString = "SELECT * FROM options WHERE RecStatus=0 AND RecID='" + Rec2Edit + "'";
            ds = GFunc.GetData(QString, "WebCNS", false);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    txtRecID.Text = row["RecID"].ToString();
                    txtOptGroup.Text = row["OptGroup"].ToString();
                    txtOptName.Text = row["OptName"].ToString();
                    txtOptValue.Text = row["OptValue"].ToString();
                    txtOptDescr.Text = row["OptDescr"].ToString();
                }
            }
            cmdClose.PostBackUrl = "#" + b.CommandArgument;
            pEdit.Visible = true;
            //throw new NotImplementedException();
        }

        protected void cmdNew_Click(object sender, EventArgs e)
        {

            lblErr.Text = "";
            txtRecID.Text = "";
            txtOptName.Text = "";
            txtOptGroup.Text = "";
            txtOptValue.Text = "";
            txtOptDescr.Text = "";

            pEdit.Visible = true;

        }

        protected void cmdClose_Click(object sender, EventArgs e)
        {
            lblErr.Text = "";
            pEdit.Visible = false;

        }

        protected void cmdDel_Click(object sender, EventArgs e)
        {
            string StrDel;

            lblErr.Text = "";
            StrDel = "UPDATE options SET RecStatus=1 WHERE RecID='" + txtRecID.Text + "'";
            GFunc.GetData(StrDel, "WebCNS", false);
            lblErr.Text = "Saraksta dati ir izdzēsti.";
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string[] c = new string[9];
            string QString = "";
            string LastID = "";
            DataSet dsLastID;

            lblErr.Text = "";

            if (string.IsNullOrEmpty(txtOptName.Text) == true)
            {
                lblErr.Text = "Nav norādīts uzstādījuma nosaukums!";
                return;
            }
            if (string.IsNullOrEmpty(ddlOptType.SelectedValue) == true)
            {
                lblErr.Text = "Nav norādīta uzstādījumu tips!";
                return;
            }
            try
            {

                c[0] = "RecID='" + GFunc.EscapeSQLCahrs(txtRecID.Text) + "'";
                c[1] = "OptType='" + GFunc.EscapeSQLCahrs(ddlOptType.SelectedValue) + "'";
                c[2] = "OptGroup='" + GFunc.EscapeSQLCahrs(txtOptGroup.Text) + "'";
                c[3] = "OptName='" + GFunc.EscapeSQLCahrs(txtOptName.Text) + "'";
                c[4] = "OptValue='" + GFunc.EscapeSQLCahrs(txtOptValue.Text) + "'";
                c[5] = "OptDescr='" + GFunc.EscapeSQLCahrs(txtOptDescr.Text) + "'";
                c[6] = "UserEdit='" + GFunc.EscapeSQLCahrs(Session["UserName"].ToString()) + "'";
                c[7] = "UserAdd='" + GFunc.EscapeSQLCahrs(Session["UserName"].ToString()) + "'";
                c[8] = "TimeAdd='" + GFunc.EscapeSQLCahrs(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) + "'";
                if (string.IsNullOrEmpty(txtRecID.Text) == false)
                {
                    QString = "UPDATE options SET " + c[1] + "," + c[2] + "," + c[3] + "," + c[4] + "," + c[5] + "," + c[6] + " WHERE " + c[0];
                }
                else
                {
                    QString = "INSERT INTO options SET " + c[1] + "," + c[2] + "," + c[3] + "," + c[4] + "," + c[5] + "," + c[6] + "," + c[7] + "," + c[8];
                }
                dsLastID = GFunc.GetData(QString, "WebCNS", true);
                if (txtRecID.Text.Length == 0)
                {

                    if (dsLastID.Tables.Count > 0)
                    {
                        if (dsLastID.Tables["tLID"].Rows.Count > 0)
                        {
                            txtRecID.Text = dsLastID.Tables["tLID"].Rows[0]["LID"].ToString();
                        }
                    }
                }
                lblErr.Text = "Uzstādījumu dati ir saglabāti.";
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
        }
    }
}
